package net.zjitc.controller;

import net.zjitc.pojo.Admin;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Goods;
import net.zjitc.pojo.UpdateAdmin;
import net.zjitc.service.AdminService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.GoodsService;
import net.zjitc.service.SettingService;
import net.zjitc.utils.APIResponse;
import net.zjitc.utils.JwtTokenUtil;
import net.zjitc.utils.Md5;
import net.zjitc.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/phone")
public class PhoneController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SettingService settingService;

    @Value("${file.upload-dir}")
    private String uploadDir;

    //admin
    @PostMapping("/admin/login")
    @ResponseBody
    public APIResponse<Admin> login(@RequestBody Admin admin){
        Admin user = adminService.ComputerLogin(admin.getUsername(),admin.getPassword());
        APIResponse<Admin> apiResponse = new APIResponse<>();
        if(user!=null){
            apiResponse.success("登录成功", user);
            return apiResponse;
        }
        apiResponse.fail(1000,"登录失败", null);
        return apiResponse;
    }
    @PostMapping("/admin/register")
    @ResponseBody
    public APIResponse<Admin> register(@RequestBody Admin admin){
        admin.setSalt("ItCast");
        String password = Md5.hashPassword(admin.getPassword(),admin.getSalt());
        admin.setPassword(password);
        APIResponse<Admin> apiResponse = new APIResponse<>();
        try{
            adminService.register(admin);
            apiResponse.success("注册成功", admin);
            return apiResponse;
        }catch (Exception e){
            apiResponse.fail(1000,"登录失败", null);
            return apiResponse;
        }
    }

    //category
    @GetMapping("/category/findAll")
    @ResponseBody
    public APIResponse<List<Category>> findAllCategory(){
        APIResponse<List<Category>> apiResponse = new APIResponse<>();
        List<Category> categoryList = categoryService.findAll();
        if(categoryList!=null){
            apiResponse.success("查询成功", categoryList);
            return apiResponse;
        }
        apiResponse.fail(1000,"查询失败", null);
        return apiResponse;
    }
    @GetMapping("/category/findOne")
    @ResponseBody
    public APIResponse<Category> findOneCategory(@RequestBody @RequestParam("id")int id){
        APIResponse<Category> apiResponse = new APIResponse<>();
        try{
            Category category = categoryService.findOne(id);
            apiResponse.success("查询成功", category);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"查询失败",null);
            return apiResponse;
        }
    }

    //goods
    @GetMapping("/goods/findAll")
    @ResponseBody
    public APIResponse<List<Goods>> findAllGoods() {
        APIResponse<List<Goods>> apiResponse = new APIResponse<>();
        List<Goods> goodsList = goodsService.findAll();
        if (goodsList != null) {
            System.out.println(goodsList);
            apiResponse.success("查询成功", goodsList);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }

    @GetMapping("/goods/findById")
    @ResponseBody
    public APIResponse<List<Goods>> findById(@RequestBody @RequestParam("id")int id) {
        APIResponse<List<Goods>> apiResponse = new APIResponse<>();
        List<Goods> goodsList = goodsService.findById(id);
        if (goodsList != null) {
            System.out.println(goodsList);
            apiResponse.success("查询成功", goodsList);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }

    @GetMapping("/goods/findOne")
    @ResponseBody
    public APIResponse<Goods> findOneGoods(@RequestBody @RequestParam("id") int id) {
        APIResponse<Goods> apiResponse = new APIResponse<>();
        Goods goods = goodsService.findOne(id);
        if (goods != null) {
            apiResponse.success("查询成功", goods);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }

    //setting
    @PostMapping("/setting/changePassword")
    @ResponseBody
    public APIResponse<String> changePassword(@RequestBody UpdateAdmin updateAdmin) {
        APIResponse<String> apiResponse = new APIResponse<>();
        int response = settingService.changePassword(
                updateAdmin.getUsername(),
                updateAdmin.getOldPassword(),
                updateAdmin.getNewPassword(),
                updateAdmin.getNewPassword2());
        if (response == 0) {
            apiResponse.success("修改成功", "0");
            return apiResponse;
        } else if (response == 1) {
            apiResponse.fail(1000, "两次输入的密码不一致", "1000");
            return apiResponse;
        } else if (response == 2) {
            apiResponse.fail(1000, "旧密码与新密码相同","1000");
            return apiResponse;
        } else if (response == 3) {
            apiResponse.fail(1000, "旧密码错误", "1000");
            return apiResponse;
        } else {
            apiResponse.fail(1000, "修改失败", "1000");
            return apiResponse;
        }

    }

    @PostMapping("/setting/changeAvatar")
    @ResponseBody
    public APIResponse<Admin> changeAvatar(@RequestBody Admin admin) {
        int admin1 = settingService.changeAvatar(admin.getUsername(),uploadDir + admin.getAvatar());
        APIResponse<Admin> apiResponse = new APIResponse<>();
        try {
            if (admin1 == 1) {
                apiResponse.fail(1000, "修改失败", null);
                return apiResponse;
            }
            apiResponse.success("修改成功", admin);
            return apiResponse;
        } catch (Exception e) {
            apiResponse.fail(1000, "修改失败", null);
            return apiResponse;
        }
    }
    @GetMapping("/findOnePage")
    @ResponseBody
    public APIResponse<List<Goods>> findOnePage(@RequestParam("pagesize") int pagesize, @RequestParam("page") int page) {
        APIResponse<List<Goods>> apiResponse = new APIResponse<>();
        List<Goods> goodsList = goodsService.findOnePage(pagesize,page);
        if (goodsList != null) {
            System.out.println(goodsList);
            apiResponse.success("查询成功", goodsList);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }
}
